﻿using System.Linq;
using System.Web.Mvc;
using Web.Models;

namespace Web.Controllers
{
    public class NewsController : Controller
    {
        // GET: News
        public ActionResult Index(int categoryId=2)
        {
            ViewBag.CategoryId = categoryId;
            using (var db = MyDbContext.Create())
            {
                ViewBag.CategoryList = db.Categories.Where(m => !m.IsDeleted && m.ParentId == CategoryIds.News).ToList();
                var model = db.News.Where(m => !m.IsDeleted && m.CategoryId == categoryId).OrderByDescending(m=>m.Id).ToList();
                return View(model);
            }
        }

        public ActionResult Detail(int? id)
        {
            if (null == id) return HttpNotFound();
            using (var db = MyDbContext.Create())
            {
                var model = db.News.SingleOrDefault(m => !m.IsDeleted && m.Id == id.Value);
                if (null == model) return HttpNotFound();
                ViewBag.CategoryList = db.Categories.Where(m => !m.IsDeleted && m.ParentId == CategoryIds.News).ToList();
                return View(model);
            }
        }
    }
}